package com.da.lon.wang.xlg.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.ModifyPhotoBean;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.http.HttpLoader;
import com.da.lon.wang.xlg.util.PreferenceUtils;
import com.da.lon.wang.xlg.util.XLGDataUtils;
import com.umeng.analytics.MobclickAgent;

import net.tsz.afinal.http.AjaxParams;

public class UpdateGenderActivity extends Activity implements OnClickListener {
    LinearLayout gender_man, gender_woman, gender_sec;
    ImageView man_select, woman_select, sec_select;
    ImageButton left;
    TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update_gender_activity);
        initView();
    }

    private void initView() {
        left = (ImageButton) findViewById(R.id.ouka_home_buyer_left);
        title = (TextView) findViewById(R.id.title);
        title.setText("修改性别");

        gender_man = (LinearLayout) findViewById(R.id.gender_man);
        gender_woman = (LinearLayout) findViewById(R.id.gender_woman);
        gender_sec = (LinearLayout) findViewById(R.id.gender_sec);
        man_select = (ImageView) findViewById(R.id.man_select);
        woman_select = (ImageView) findViewById(R.id.woman_select);
        sec_select = (ImageView) findViewById(R.id.sec_select);

        left.setOnClickListener(this);
        gender_man.setOnClickListener(this);
        gender_woman.setOnClickListener(this);
        gender_sec.setOnClickListener(this);
        initImage();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private void initImage() {
        try {
            String gender = PreferenceUtils.getInstance().getSettingStr(
                    PreferencesConfig.USER_SEX, "");
            if (gender != null && !"".equals(gender)) {
                switch (gender) {
                    case "1":
                        man_select.setVisibility(View.VISIBLE);
                        woman_select.setVisibility(View.INVISIBLE);
                        sec_select.setVisibility(View.INVISIBLE);
                        break;
                    case "0":
                        man_select.setVisibility(View.INVISIBLE);
                        woman_select.setVisibility(View.VISIBLE);
                        sec_select.setVisibility(View.INVISIBLE);
                        break;
                    case "2":
                        man_select.setVisibility(View.INVISIBLE);
                        woman_select.setVisibility(View.INVISIBLE);
                        sec_select.setVisibility(View.VISIBLE);
                        break;
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

    }

    @Override
    public void onClick(View v) {
        String gender = null;
        switch (v.getId()) {
            case R.id.gender_man:
                man_select.setVisibility(View.VISIBLE);
                woman_select.setVisibility(View.INVISIBLE);
                sec_select.setVisibility(View.INVISIBLE);
                gender = "1";
                break;
            case R.id.gender_woman:
                man_select.setVisibility(View.INVISIBLE);
                woman_select.setVisibility(View.VISIBLE);
                sec_select.setVisibility(View.INVISIBLE);
                gender = "0";
                break;
            case R.id.gender_sec:
                man_select.setVisibility(View.INVISIBLE);
                woman_select.setVisibility(View.INVISIBLE);
                sec_select.setVisibility(View.VISIBLE);
                gender = "2";
                break;
            case R.id.ouka_home_buyer_left:
                finish();
                break;
        }
        if (gender != null && !"".equals(gender)) {
            update(gender);
        }
    }


    private void update(String gender) {
        HttpLoader<ModifyPhotoBean> httpLoader = new HttpLoader<ModifyPhotoBean>(
                this, XLGConstants.URL + "User-userinfoedit",
                ModifyPhotoBean.class);
        AjaxParams params = new AjaxParams();
        params.put("token", XLGDataUtils.encrypt2MD5("User" + "2015#$%"
                + "userinfoedit"));
        params.put(
                "id",
                PreferenceUtils.getInstance().getSettingStr(
                        PreferencesConfig.USER_ID, ""));
        params.put("device_type", "1");
        params.put(
                "deviceId",
                PreferenceUtils.getInstance().getSettingStr(
                        PreferencesConfig.DEVICE_ID, ""));
        params.put(
                "user_key",
                PreferenceUtils.getInstance().getSettingStr(
                        PreferencesConfig.USER_KEY, ""));
        params.put("sex", gender);
        params.put("birth_year",
                PreferenceUtils.getInstance().getSettingStr("birth_year", ""));
        params.put("birth_month",
                PreferenceUtils.getInstance().getSettingStr("birth_month", ""));
        params.put("birth_day",
                PreferenceUtils.getInstance().getSettingStr("birth_day", ""));
        params.put(
                "nickname",
                PreferenceUtils.getInstance().getSettingStr(
                        PreferencesConfig.USER_NAME, ""));
        httpLoader.loadData(params);
        PreferenceUtils.getInstance().SetSettingString(
                PreferencesConfig.USER_SEX, gender);
        this.finish();


    }
}
